package com.qf.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;

/**
 * @author sunyamei
 * @version V1.0
 * @Project springmvc_Demo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/14 15:40
 */
@Controller
public class DownLoadController {

    @RequestMapping("/downLoad")
    public void downLoad(String name, HttpSession session, HttpServletResponse response)throws Exception{
        System.out.println("name = " + name);
        String baselPath = session.getServletContext().getRealPath("/upload");
        String filePath=baselPath+ File.separator+name;//upload/101.jpg
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名 ***
        response.setHeader("content-disposition","attachment;filename="+name);
        //响应
        IOUtils.copy(new FileInputStream(filePath),response.getOutputStream());
    }


}
